package by.grsu.coctails.fragments;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import by.grsu.coctails.R;
import by.grsu.coctails.adapter.AbstractAdapter;
import by.grsu.coctails.common.utils.Log;
import by.grsu.coctails.db.DBManager;
import by.grsu.coctails.db.task.CommonGetAsyncTask;
import by.grsu.coctails.db.task.GetCoctailsCategoryAsyncTask;
import by.grsu.coctails.db.task.GetIngredientsCategoryAsyncTask;
import by.grsu.coctails.model.db.CoctailsCategoryDb;
import by.grsu.coctails.model.db.IngredientCategoryDb;

import com.google.android.imageloader.ImageLoader;

public abstract class CommonDBFragment<B> extends CommonReceiverFragment
		implements OnItemSelectedListener {

	private static final String TAG = CommonDBFragment.class.getSimpleName();

	private DBManager dbManager;

	private List<CharSequence> categoryList;

	private AbstractAdapter<B> mAdapter;

	private ListView mList;

	private ImageLoader mImageLoader;

	private String mCurrentCategoryName;

	private TextView mTitle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbManager = DBManager.get(getActivity());
		categoryList = new ArrayList<CharSequence>();
		setRetainInstance(true);
		setHasOptionsMenu(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mImageLoader = ImageLoader.get(getActivity());
		mAdapter = createAdapter();
		mList = (ListView) getView().findViewById(R.id.list);
		mList.setAdapter(mAdapter);
		mTitle = (TextView) getView().findViewById(R.id.title);
		mTitle.setText(getTitle());
		initCategory();
		registerReceiver();

	}

	@Override
	public void onStop() {
		super.onStop();
		unRegisterReceiver();
	}

	public DBManager getDbManager() {
		return dbManager;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(getLayoutResource(), container, false);
	}

	private synchronized void initCategory() {
		Log.d(TAG, "start init category");
		if (mAdapter != null && mAdapter.getList().size() == 0) {
			if (this instanceof ListCoctailFragment
					|| this instanceof MyCoctailsFragment) {
				new GetCoctailsCategoryAsyncTask(getActivity()) {

					@Override
					public void onComplete(List<CoctailsCategoryDb> result) {

						categoryList.clear();
						categoryList.add(getString(R.string.all));
						for (CoctailsCategoryDb categoryCoctail : result) {
							categoryList.add(categoryCoctail.getName());

						}

						initCategory(categoryList);

					}
				}.start();

			}

			if (this instanceof ListIngredientFragment) {
				new GetIngredientsCategoryAsyncTask(getActivity()) {

					@Override
					public void onComplete(List<IngredientCategoryDb> result) {
						categoryList.clear();
						categoryList.add(getString(R.string.all));
						for (IngredientCategoryDb ingredientCategory : result) {
							categoryList.add(ingredientCategory.getName());

						}

						initCategory(categoryList);

					}
				}.start();
			}
		}

	}

	private synchronized void initCategory(List<CharSequence> list) {

		Log.d(TAG, "category init list =" + list.size());

		Spinner spinner = (Spinner) getView().findViewById(R.id.category);
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				getActivity(), R.layout.spinner_header, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
	}

	public AbstractAdapter<B> getAdapter() {
		return mAdapter;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		mAdapter.getList().clear();
		mCurrentCategoryName = (String) categoryList.get(position);
		if (position == 0) {
			mCurrentCategoryName = "";
		}
		getTask(mCurrentCategoryName).start();
	}

	protected void reloadDataFromDB() {
		getAdapter().clear();
		getTask(mCurrentCategoryName).start();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	public ListView getListView() {
		return mList;
	}

	public abstract String getTitle();

	public abstract AbstractAdapter<B> createAdapter();

	public abstract CommonGetAsyncTask<B> getTask(String categoryName);

	public abstract int getLayoutResource();

}
